/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package com.lineage.server.model.item.action;

import java.lang.reflect.Constructor;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.w3c.dom.Element;

import com.lineage.server.utils.collections.Maps;

/**
 * Class <code>L1ItemActionFactory</code> 道具动作工厂.
 * 
 * @author jrwz
 * @version 2012-6-20下午03:29:44
 * @see com.lineage.server.model.item.action
 * @since JDK1.7
 */
public final class L1ItemActionFactory {
    /** 提示信息. */
    private static final Logger LOG = Logger
            .getLogger(L1ItemActionFactory.class.getName());
    /** 动作集合. */
    private static Map<String, Constructor<? extends L1ItemXmlAction>> ACTIONS = Maps
            .newMap();

    static {
        try {
            ACTIONS.put("动作", loadConstructor(L1ItemListedAction.class));
            ACTIONS.put("加血", loadConstructor(L1ItemAddHpAction.class));
            ACTIONS.put("加魔", loadConstructor(L1ItemAddMpAction.class));
            ACTIONS.put("回魔", loadConstructor(L1ItemReMpAction.class));
            ACTIONS.put("增加魔攻", loadConstructor(L1ItemAddSpAction.class));
            ACTIONS.put("水底呼吸", loadConstructor(L1ItemAddEvaAction.class));
            ACTIONS.put("变身", loadConstructor(L1ItemPolyAction.class));
            ACTIONS.put("技能效果_闇盲咒术",
                    loadConstructor(L1ItemSkillEffectsAction.class));
            ACTIONS.put("加速", loadConstructor(L1ItemSpeedAction.class));
            ACTIONS.put("判断", loadConstructor(L1ItemCheckAction.class));
            ACTIONS.put("特效", loadConstructor(L1ItemSkillGfxAction.class));
            ACTIONS.put("技能", loadConstructor(L1ItemSkillEffectAction.class));
            ACTIONS.put("经验", loadConstructor(L1ItemAddExpAction.class));
        } catch (final NoSuchMethodException e) {
            LOG.log(Level.SEVERE, "ItemAction类加载失败", e);
        }
    }

    /**
     * 加载.
     * 
     * @param c
     *            - 道具XML动作
     * @return Class
     * @throws NoSuchMethodException
     *             -
     */
    private static Constructor<? extends L1ItemXmlAction> loadConstructor(
            final Class<? extends L1ItemXmlAction> c)
            throws NoSuchMethodException {
        return c.getConstructor(new Class[] { Element.class });
    }

    /**
     * 新动作.
     * 
     * @param element
     *            - XML 文档中的一个元素
     * @return 类的新实例 没有则返回null
     */
    public static L1ItemAction newAction(final Element element) {
        try {
            final Constructor<? extends L1ItemXmlAction> con = ACTIONS
                    .get(element.getNodeName());
            return con.newInstance(element);
        } catch (final NullPointerException e) {
            LOG.warning(element.getNodeName() + " 未定义的Item动作.");
        } catch (final Exception e) {
            LOG.log(Level.SEVERE, "ItemAction类加载失败", e);
        }
        return null;
    }

    /** 道具动作工厂. */
    private L1ItemActionFactory() {
    }
}
